import { getCookie } from '@/utils/token'
import { defineAsyncComponent } from 'vue'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
// 路由
const routes:RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/blog'
  },
  {
    path: '/home',
    name: 'home',
    // component: () => import("@/views/home/index.vue")
    component: defineAsyncComponent(() => import("@/views/home/index.vue"))
  },
  {
    path: '/blog',
    name: 'blog',
    component: () => import("@/views/blog/index.vue")
  },
  {
    path: '/blogDetail',
    name: 'blogDetail',
    component: defineAsyncComponent(() => import("@/views/blog/blogDetail.vue"))
  },
  {
    path: '/photos',
    name: 'photos',
    // component: () => import("@/views/photos/index.vue")
    component: defineAsyncComponent(() => import("@/views/photos/index.vue"))
  },
  {
    path: '/blogEdit',
    name: 'blogEdit',
    // component: () => import("@/views/add-edit-article/index.vue")
    component: defineAsyncComponent(() => import("@/views/add-edit-article/index.vue"))
  },
  {
    path: '/userCenter',
    name: 'userCenter',
    // component: () => import("@/views/userCenter/index.vue")
    component: defineAsyncComponent(() => import("@/views/userCenter/index.vue"))
  },
  {
    path: '/daily',
    name: 'daily',
    // component: () => import("@/views/daily/index.vue")
    component: defineAsyncComponent(() => import("@/views/daily/index.vue"))
  },
  {
    path: '/news',
    name: 'news',
    // component: () => import("@/views/news/index.vue")
    component: defineAsyncComponent(() => import("@/views/news/index.vue"))
  },
  {
    path: '/ai',
    name: 'ai',
    // component: () => import("@/views/ai/index.vue")
    component: defineAsyncComponent(() => import("@/views/ai/index.vue"))
  },
  {
    path: '/star',
    name: 'star',
    // component: () => import("@/views/star/index.vue")
    component: defineAsyncComponent(() => import("@/views/star/index.vue"))
  },
  {
    path: '/about',
    name: 'about',
    // component: () => import("@/views/about/index.vue")
    component: defineAsyncComponent(() => import("@/views/about/index.vue"))
  },
  {
    path: '/category',
    name: 'category',
    // component: () => import("@/views/category/index.vue")
    component: defineAsyncComponent(() => import("@/views/category/index.vue"))
  }
]
const route = createRouter({
  history: createWebHashHistory(),
  routes,
})
// 路由导航守卫

/*
  @@ 打包后本地用的file协议，不能操作cookie
*/
route.beforeEach((to:any,from:any) =>{
  const cookie = getCookie('accessToken')
  if(!cookie && to.name == 'userCenter') {
    return {name: 'blog'}
  } 
})
export default route
